<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="ctx" value="${pageContext.request.contextPath}" />
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户登录 - 商品评价系统</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <style>
        body {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            display: flex;
            align-items: center;
        }
        .login-container {
            background: white;
            border-radius: 15px;
            box-shadow: 0 15px 35px rgba(0,0,0,0.1);
            overflow: hidden;
        }
        .login-form {
            padding: 60px 40px;
        }
        .login-image {
            background: linear-gradient(45deg, #667eea 0%, #764ba2 100%);
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
        }
        .form-control {
            border-radius: 10px;
            border: 2px solid #e9ecef;
            padding: 12px 15px;
        }
        .form-control:focus {
            border-color: #667eea;
            box-shadow: 0 0 0 0.2rem rgba(102, 126, 234, 0.25);
        }
        .btn-login {
            background: linear-gradient(45deg, #667eea 0%, #764ba2 100%);
            border: none;
            border-radius: 10px;
            padding: 12px;
            font-weight: 600;
        }
        .btn-login:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4);
        }
        .alert {
            border-radius: 10px;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-lg-10">
                <div class="login-container row g-0">
                    <!-- 左侧图片区域 -->
                    <div class="col-lg-6 login-image">
                        <div class="text-center">
                            <i class="fas fa-shopping-cart fa-5x mb-4"></i>
                            <h2>商品评价系统</h2>
                            <p class="lead">发现优质商品，分享真实评价</p>
                        </div>
                    </div>
                    
                    <!-- 右侧登录表单 -->
                    <div class="col-lg-6">
                        <div class="login-form">
                            <div class="text-center mb-4">
                                <h3 class="fw-bold">欢迎回来</h3>
                                <p class="text-muted">请登录您的账户</p>
                            </div>
                            
                            <!-- 错误提示 -->
                            <div id="errorAlert" class="alert alert-danger d-none" role="alert">
                                <i class="fas fa-exclamation-circle me-2"></i>
                                <span id="errorMessage"></span>
                            </div>
                            
                            <!-- 登录表单 -->
                            <form id="loginForm">
                                <div class="mb-3">
                                    <label for="username" class="form-label">
                                        <i class="fas fa-user me-2"></i>用户名
                                    </label>
                                    <input type="text" class="form-control" id="username" name="username" 
                                           placeholder="请输入用户名" required>
                                </div>
                                
                                <div class="mb-3">
                                    <label for="password" class="form-label">
                                        <i class="fas fa-lock me-2"></i>密码
                                    </label>
                                    <div class="input-group">
                                        <input type="password" class="form-control" id="password" name="password" 
                                               placeholder="请输入密码" required>
                                        <button class="btn btn-outline-secondary" type="button" id="togglePassword">
                                            <i class="fas fa-eye"></i>
                                        </button>
                                    </div>
                                </div>
                                
                                <div class="mb-3 form-check">
                                    <input type="checkbox" class="form-check-input" id="rememberMe">
                                    <label class="form-check-label" for="rememberMe">
                                        记住我
                                    </label>
                                </div>
                                
                                <button type="submit" class="btn btn-primary btn-login w-100 mb-3">
                                    <i class="fas fa-sign-in-alt me-2"></i>登录
                                </button>
                            </form>
                            
                            <div class="text-center">
                                <p class="mb-0">
                                    还没有账户？
                                    <a href="${ctx}/page/register" class="text-decoration-none">
                                        立即注册
                                    </a>
                                </p>
                                <a href="${ctx}/page/index" class="text-muted text-decoration-none mt-2 d-block">
                                    <i class="fas fa-arrow-left me-2"></i>返回首页
                                </a>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        window.CONTEXT_PATH = '${pageContext.request.contextPath}';
    </script>
    <script src="${ctx}/static/js/config.js"></script>
    <script src="${ctx}/static/js/common.js"></script>
    <script>
        
        $(document).ready(function() {
            // 检查是否已登录
            checkLoginStatus();
            
            // 密码显示/隐藏切换
            $('#togglePassword').click(function() {
                const passwordField = $('#password');
                const icon = $(this).find('i');
                
                if (passwordField.attr('type') === 'password') {
                    passwordField.attr('type', 'text');
                    icon.removeClass('fa-eye').addClass('fa-eye-slash');
                } else {
                    passwordField.attr('type', 'password');
                    icon.removeClass('fa-eye-slash').addClass('fa-eye');
                }
            });
            
            // 登录表单提交
            $('#loginForm').submit(function(e) {
                e.preventDefault();
                
                const username = $('#username').val().trim();
                const password = $('#password').val();
                
                // 基本验证
                if (!username) {
                    showError('请输入用户名');
                    return;
                }
                
                if (!password) {
                    showError('请输入密码');
                    return;
                }
                
                // 提交登录请求
                login(username, password);
            });
        });
        
        // 检查登录状态
        function checkLoginStatus() {
            apiRequest({
                url: AppConfig.APIS.USER.CURRENT,
                type: 'GET',
                success: function(result) {
                    if (result.code === 200 && result.data) {
                        // 已登录，跳转到首页
                        goToPage(AppConfig.PAGES.INDEX);
                    }
                }
            });
        }
        
        // 登录函数
        function login(username, password) {
            // 显示加载状态
            const submitBtn = $('#loginForm button[type="submit"]');
            const originalText = submitBtn.html();
            submitBtn.html('<i class="fas fa-spinner fa-spin me-2"></i>登录中...').prop('disabled', true);
            
            apiRequest({
                url: AppConfig.APIS.USER.LOGIN,
                type: 'POST',
                data: JSON.stringify({
                    username: username,
                    password: password
                }),
                success: function(result) {
                    if (result.code === 200) {
                        // 登录成功
                        showSuccess('登录成功，正在跳转...');
                        setTimeout(function() {
                            // 检查是否是管理员
                            if (result.data && result.data.username === 'admin') {
                                goToPage(AppConfig.PAGES.ADMIN);
                            } else {
                                goToPage(AppConfig.PAGES.INDEX);
                            }
                        }, 1000);
                    } else {
                        showError(result.message || '登录失败');
                    }
                },
                error: function(xhr) {
                    let errorMsg = '登录失败';
                    if (xhr.responseJSON && xhr.responseJSON.message) {
                        errorMsg = xhr.responseJSON.message;
                    } else if (xhr.status === 401) {
                        errorMsg = '用户名或密码错误';
                    } else if (xhr.status === 403) {
                        errorMsg = '账户已被禁用';
                    }
                    showError(errorMsg);
                },
                complete: function() {
                    // 恢复按钮状态
                    submitBtn.html(originalText).prop('disabled', false);
                }
            });
        }
        
        // 显示错误信息
        function showError(message) {
            $('#errorMessage').text(message);
            $('#errorAlert').removeClass('d-none alert-success').addClass('alert-danger');
            
            // 3秒后自动隐藏
            setTimeout(function() {
                $('#errorAlert').addClass('d-none');
            }, 3000);
        }
        
        // 显示成功信息
        function showSuccess(message) {
            $('#errorMessage').text(message);
            $('#errorAlert').removeClass('d-none alert-danger').addClass('alert-success');
        }
        
        // 回车键登录
        $(document).keypress(function(e) {
            if (e.which === 13) {
                $('#loginForm').submit();
            }
        });
    </script>
</body>
</html>